package com.youhome.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by chen365 on 2017/7/20.
 */
public class DateUtils {

    /**
     * 日期转换成字符串
     *
     * @param date
     * @return str
     */
    public static String DateToStr(Date date) {
        if (date == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }


    /**
     * 日期转换成短日期
     *
     * @param date
     * @return str
     */
    public static Date DateToDateline(Date date) {
        if (date == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return format.parse(format.format(date));
        } catch (ParseException e) {
            return null;
        }
    }


    /**
     * 日期转换成字符串
     *
     * @param date
     * @return str
     */
    public static String DateToStr(Date date, String pattern) {
        if (date == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String str = format.format(date);
        return str;
    }

    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 取得指定日期格式的字符串
     *
     * @param date
     * @return String
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }



    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date StrToDate(String str, String pattern) {

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    public static Date addMonth(String time, int count) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(DateUtils.StrToDate(time, "yyyy-MM-dd"));
        calendar.add(calendar.MONTH, count);//把日期往后增加一个月.整数往后推,负数往前移动
        return calendar.getTime();
    }

    public static Date addMonth(Date time, int count) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(time);
        calendar.add(calendar.MONTH, count);//把日期往后增加一个月.整数往后推,负数往前移动
        return calendar.getTime();
    }

    public static Date addYear(String time, int count) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(DateUtils.StrToDate(time, "yyyy-MM-dd"));
        calendar.add(calendar.YEAR, count);//把日期往后增加一年.整数往后推,负数往前移动
        return calendar.getTime();
    }

    public static Date addYear(Date time, int count) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(time);
        calendar.add(calendar.YEAR, count);//把日期往后增加一年.整数往后推,负数往前移动
        return calendar.getTime();
    }


//    public static void main(String[] args) {
//        System.err.println(DateToDateline(new Date()));
//    }


}
